﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TCOChart
{
    public partial class FormMachineMES : Form
    {
        public DataMESRegisterClass data = new DataMESRegisterClass();
        private LogHelper _logHelper;

        public FormMachineMES()
        {
            InitializeComponent();
            _logHelper = new LogHelper(richTextBox_log);

            string path = Path.Combine(Environment.CurrentDirectory, "MesData.json");
            string jsonStr3 = "";
            if (File.Exists(path))
            {
                jsonStr3 = File.ReadAllText(path);
            }

            data.FromJson(jsonStr3);
            _logHelper.Log("数据已加载...");
            input_machineSN.Text = data.registerData.MachineSn;
            input_floor.Text = data.registerData.Floor;
        }

        private void button_register_Click(object sender, EventArgs e)
        {
            data.registerData.MachineSn = input_machineSN.Text;
            data.registerData.Floor = input_floor.Text;
            data.registerData.OsVersion = input_osVersion.Text;

            string jsonStr = JsonConvert.SerializeObject(data.registerData, Formatting.Indented);

            // 保存到文件
            string filePath = "MesData.json"; // 文件路径
            File.WriteAllText(filePath, jsonStr);
            AntdUI.Modal.open(new AntdUI.Modal.Config(this, "保存成功", new AntdUI.Modal.TextLine[] {
                    new AntdUI.Modal.TextLine("数据保存成功")
                    }, AntdUI.TType.Success)
            {
                CancelText = null,
                OkType = AntdUI.TTypeMini.Success,
                OkText = "OK",

            });
            _logHelper.Log("数据保存成功");
        }
    }


    public class MESRegisterData
    {
        public string MachineSn;
        public string Floor;
        public string OsVersion;
        public string MachineName;
        public string LineCode;
        public string AppVersion;
        public string MachineType;
        public string UnitCode;
        public string ConfigVersion;
        public string FunctionId;
        public string StationName;
        public string AddBy;
        public string ManageDept;
        public string StationType;
        public string EditBy;
        public string RegisterType;
        public string StationId;
        public string AddData;
        public string Product;
        public string VendorName;
        public string EditData;
        public string SiteCode;
        public string DeviceIp;
        public string From;
        public string Area;
        public string DeviceMac;
        public string ApiVersion;
    }

    public class DataMESRegisterClass
    {
        public MESRegisterData registerData {  get; set; } = new MESRegisterData();

        public DataMESRegisterClass()
        {}

        public void FromJson(string jsonStr)
        {
            try
            {

                var data = JObject.Parse(jsonStr);
                registerData.MachineSn = data["MachineSn"].ToString();
                registerData.Floor = data["Floor"].ToString();
                registerData.OsVersion = data["OsVersion"].ToString();
                // .......
                
            }
            catch (JsonReaderException e)
            {
                
            }
        }
    }
}
